import {constantRoutes} from '@/router'
import {getRouters} from '@/api/menu'
import Layout from '@/layout/index'
// import logoImg from '@/assets/logo/logo.png'
const permission = {
  state: {
    splitStr:"||||",
    sysName:'源网荷储一体化管控平台',//系统名称
    logo:"", //logo图片
    pageUrl:"/bigScreen/productCarbon", //logo跳转地址
    routes: [],
    addRoutes: [],
    //产品碳足迹
    bigScreen: [
      {
        path: '/bigScreen',//首页
        component: (resolve) => require(['@/views/bigScreen/views/App'], resolve),
        name: 'bigScreen/index',
        hidden: true,
        children: [
          {
            path: 'productCarbon',
            hidden: true,
            name: 'b_productCarbon',
            component: (resolve) => require(['@/views/bigScreen/carbon/productCarbon/index'], resolve),
            meta: { title: '碳足迹计算', icon: 'dashboard', noCache: true, affix: true, bigScreen: true }
          },
          {
            path: 'product',
            hidden: true,
            component: (resolve) => require(['@/views/bigScreen/views/App'], resolve),
            name: 'b_product',
            meta: { title: '产品配置', icon: 'dashboard', noCache: true, affix: true, bigScreen: true },
            children: [{
              path: 'productCategorize',
              hidden: true,
              name: 'b_productCategorize',
              meta: { title: '产品分类', icon: 'report', noCache: true, affix: true, bigScreen: true },
              component: (resolve) => require(['@/views/bigScreen/productCategorize/index'], resolve),
            },{
              path: 'standardStage',
              hidden: true,
              name: 'b_standardStage',
              meta: { title: '标准阶段', icon: 'report', noCache: true, affix: true, bigScreen: true },
              component: (resolve) => require(['@/views/bigScreen/standardStage/index'], resolve),
            }]
          },
          {
            path: 'categorize',
            hidden: true,
            component: (resolve) => require(['@/views/bigScreen/views/App'], resolve),
            name: 'b_categorize',
            meta: { title: '因子库配置', icon: 'dashboard', noCache: true, affix: true, bigScreen: true },
            children: [{
              path: 'categorizeInfo',
              hidden: true,
              name: 'b_categorizeInfo',
              meta: { title: '因子特征要素', icon: 'report', noCache: true, affix: true, bigScreen: true },
              component: (resolve) => require(['@/views/bigScreen/categorizeInfo/index'], resolve),
            },{
              path: 'carbonFactorInfo',
              hidden: true,
              name: 'b_carbonFactorInfo',
              meta: { title: '碳排放因子库', icon: 'report', noCache: true, affix: true, bigScreen: true },
              component: (resolve) => require(['@/views/bigScreen/carbonFactorInfo/index'], resolve),
            }]
          },
          {
            path: 'productInfoAll',
            hidden: true,
            component: (resolve) => require(['@/views/bigScreen/views/App'], resolve),
            name: 'b_productInfoAll',
            meta: { title: '碳排放核算', icon: 'dashboard', noCache: true, affix: true, bigScreen: true },
            children: [{
              path: 'productInfo',
              hidden: true,
              name: 'b_productInfo',
              meta: { title: '产品周期要素', icon: 'report', noCache: true, affix: true, bigScreen: true },
              component: (resolve) => require(['@/views/bigScreen/productInfo/index'], resolve),
            },{
              path: 'emissionPathLog',
              hidden: true,
              name: 'b_emissionPathLog',
              meta: { title: '减排路径记录', icon: 'report', noCache: true, affix: true, bigScreen: true },
              component: (resolve) => require(['@/views/bigScreen/carbon/emissionPathLog/index'], resolve),
            },{
              path: 'productCarbonConChart',
              hidden: true,
              name: 'b_productCarbonConChart',
              meta: { title: '历史查询', icon: 'report', noCache: true, affix: true, bigScreen: true },
              component: (resolve) => require(['@/views/bigScreen/carbon/productCarbon/ProductCarbonConChart'], resolve),
            }]
          },
        ]
      }
  ],

  },
  mutations: {
    SET_ROUTES: (state, routes) => {
      state.addRoutes = routes;
      state.routes = constantRoutes.concat(routes)
    },
    SET_LOGO: (state, logo) => {
      state.logo = logo;
    },
    SET_SYS_NAME: (state, sysName) => {
      state.sysName = sysName;
    },
    SET_BIG_SCREEN: (state, data) => {
      state.bigScreen = data;
    }
  },
  actions: {
    // 生成路由
    GenerateRoutes({commit}) {
      return new Promise(resolve => {
          getRouters().then(res => {
            const accessedRoutes = filterAsyncRouter(res.data);
            accessedRoutes.push({path: '*', redirect: '/404', hidden: true});
            commit('SET_ROUTES', accessedRoutes);
            resolve(accessedRoutes)
          })
      })
    },
  }
};

// 遍历后台传来的路由字符串，转换为组件对象
function filterAsyncRouter(asyncRouterMap) {
  return asyncRouterMap.filter(route => {
    if (route.component) {
      // Layout组件特殊处理
      if (route.component == 'Layout') {
        route.component = Layout
      } else {
        route.component = loadView(route.component)
      }
    }
    if (route.children != null && route.children && route.children.length) {
      route.children = filterAsyncRouter(route.children)
    }
    route.meta.customMenuType = route.customMenuType;
    route.meta.frontPageType = route.frontPageType;
    route.meta.isMonitor = Number(route.customMenuType) === 1;
    return true
  })
}

export const loadView = (view) => { // 路由懒加载
  return (resolve) => require([`@/views/${view}`], resolve)
};

export default permission
